package app.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.TimeZone;

public class DateUtil {
	public static final String Format_Date = "yyyy-MM-dd";
	public static final String Format_Time = "HH:mm:ss";
	public static final String Format_DateTime = "yyyy-MM-dd HH:mm:ss";
	public static final String Format_DateTimeWithoutMis = "yyyy-MM-dd HH:mm";
	public static String FORMART_YYYYMMDD = "yyyyMMDD";

	public static String getCurrentDate() {
		return new SimpleDateFormat("yyyy-MM-dd").format(new Date());
	}

	public static String getCurrentDate(String format) {
		SimpleDateFormat t = new SimpleDateFormat(format);
		return t.format(new Date());
	}

	public static String getCurrentTime() {
		return new SimpleDateFormat("HH:mm:ss").format(new Date());
	}

	public static String getCurrentTime(String format) {
		SimpleDateFormat t = new SimpleDateFormat(format);
		return t.format(new Date());
	}

	public static String getCurrentDateTime() {
		String format = "yyyy-MM-dd HH:mm:ss";
		return getCurrentDateTime(format);
	}

	public static int getDayOfWeek() {
		Calendar cal = Calendar.getInstance();
		return cal.get(7);
	}

	public static int getDayOfWeek(Date date) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		return cal.get(7);
	}
	
	/**
	 * 获取每月的所有周的开始时间和结束时间
	 * @param year
	 * @param month
	 * @return
	 * 第1周
	 * 开始：2018-04-30
	 * 结束：2018-05-06
	 */
	public static List<String> getAllWeekOfMonth(int year,int month) {
		List<String> returnList = new ArrayList<String>();
		Calendar calendar = Calendar.getInstance();
		calendar.set(Calendar.YEAR, year);
		calendar.set(Calendar.MONTH	,month-1);
		calendar.set(Calendar.DATE, 1);
		calendar.getTime();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		int actualMaximum = calendar.getActualMaximum(Calendar.WEEK_OF_MONTH);
		calendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
		for (int i = 1; i < actualMaximum+1; i++) {
			String str = "第"+i+"周("+sdf.format(calendar.getTime());
			calendar.add(Calendar.DATE, 7);
			Calendar tempCal = Calendar.getInstance();
			tempCal.setTime(calendar.getTime());
			tempCal.add(Calendar.DATE, -1);
			str += " ~ "+sdf.format(tempCal.getTime())+")";
			returnList.add(str);
		}
		return returnList;
	}

	public static int getDayOfMonth() {
		Calendar cal = Calendar.getInstance();
		return cal.get(5);
	}

	public static int getDayOfMonth(Date date) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		return cal.get(5);
	}

	public static int getMaxDayOfMonth(Date date) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		return cal.getActualMaximum(5);
	}

	public static String getFirstDayOfMonth(String date) {
		return getFirstDayOfMonth(parse(date));
	}

	public static String getFirstDayOfMonth(Date date) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.set(5, 1);
		return new SimpleDateFormat("yyyy-MM-dd").format(cal.getTime());
	}

	public static String getLastDayOfMonth(String date) {
		return getLastDayOfMonth(parse(date));
	}

	public static String getLastDayOfMonth(Date date) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.set(5, cal.getActualMaximum(5));
		return new SimpleDateFormat("yyyy-MM-dd").format(cal.getTime());
	}

	public static int getDayOfYear() {
		Calendar cal = Calendar.getInstance();
		return cal.get(6);
	}

	public static int getDayOfYear(Date date) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		return cal.get(6);
	}

	public static int getDayOfWeek(String date) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(parse(date));
		return cal.get(7);
	}

	public static int getDayOfMonth(String date) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(parse(date));
		return cal.get(5);
	}

	public static int getDayOfYear(String date) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(parse(date));
		return cal.get(6);
	}

	public static String getCurrentDateTime(String format) {
		SimpleDateFormat t = new SimpleDateFormat(format);
		return t.format(new Date());
	}

	public static String toDateTimeString(Date date) {
		if (date == null) {
			return "";
		}
		return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date);
	}

	public static String toDateTimeString(Date date, TimeZone timeZone) {
		return toString(date, "yyyy-MM-dd HH:mm:ss", timeZone);
	}

	@Deprecated
	public static String time2Str(Date date, String pattern, TimeZone timeZone) {
		return toString(date, pattern, timeZone);
	}

	@Deprecated
	public static String time2Str(Date date, TimeZone timeZone) {
		return toDateTimeString(date, timeZone);
	}

	@Deprecated
	public static String time2Str(Date date, String pattern) {
		return toString(date, pattern);
	}

	@Deprecated
	public static String time2Str(Date date) {
		return toDateTimeString(date);
	}

	public static String toString(Date date, String format, TimeZone timeZone) {
		SimpleDateFormat t = new SimpleDateFormat(format);
		t.setTimeZone(timeZone);
		return t.format(date);
	}

	public static String toString(Date date, TimeZone timeZone) {
		return toString(date, "yyyy-MM-dd", timeZone);
	}

	public static String toString(Date date, String pattern) {
		return toString(date, pattern, TimeZone.getTimeZone("Asia/Shanghai"));
	}

	public static String toString(Date date) {
		return toString(date, "yyyy-MM-dd");
	}

	@Deprecated
	public static String date2Str(Date date) {
		return toString(date);
	}

	@Deprecated
	public static String date2Str(Date date, String pattern) {
		return toString(date, pattern);
	}

	@Deprecated
	public static String date2Str(Date date, TimeZone timeZone) {
		return toString(date, timeZone);
	}

	@Deprecated
	public static String date2Str(Date date, String format, TimeZone timeZone) {
		return toString(date, format, timeZone);
	}

	public static String toTimeString(Date date) {
		if (date == null) {
			return "";
		}
		return new SimpleDateFormat("HH:mm:ss").format(date);
	}

	public static int compare(String date1, String date2) throws ParseException {
		return compare(date1, date2, "yyyy-MM-dd");
	}

	public static int compareTime(String time1, String time2) {
		return compareTime(time1, time2, "HH:mm:ss");
	}

	public static int compare(String date1, String date2, String format) throws ParseException {
		Date d1 = parse(date1, format);
		Date d2 = parse(date2, format);
		return d1.compareTo(d2);
	}

	public static int compareTime(String time1, String time2, String format) {
		String[] arr1 = time1.split(":");
		String[] arr2 = time2.split(":");
		if (arr1.length < 2) {
			throw new RuntimeException("错误的时间值:" + time1);
		}
		if (arr2.length < 2) {
			throw new RuntimeException("错误的时间值:" + time2);
		}
		int h1 = Integer.parseInt(arr1[0]);
		int m1 = Integer.parseInt(arr1[1]);
		int h2 = Integer.parseInt(arr2[0]);
		int m2 = Integer.parseInt(arr2[1]);
		int s1 = 0;
		int s2 = 0;
		if (arr1.length == 3) {
			s1 = Integer.parseInt(arr1[2]);
		}
		if (arr2.length == 3) {
			s2 = Integer.parseInt(arr2[2]);
		}
		if ((h1 < 0) || (h1 > 23) || (m1 < 0) || (m1 > 59) || (s1 < 0) || (s1 > 59)) {
			throw new RuntimeException("错误的时间值:" + time1);
		}
		if ((h2 < 0) || (h2 > 23) || (m2 < 0) || (m2 > 59) || (s2 < 0) || (s2 > 59)) {
			throw new RuntimeException("错误的时间值:" + time2);
		}
		if (h1 != h2) {
			return h1 > h2 ? 1 : -1;
		}
		if (m1 == m2) {
			if (s1 == s2) {
				return 0;
			}
			return s1 > s2 ? 1 : -1;
		}
		return m1 > m2 ? 1 : -1;
	}

	public static boolean isTime(String time) {
		String[] arr = time.split(":");
		if (arr.length < 2) {
			return false;
		}
		try {
			int h = Integer.parseInt(arr[0]);
			int m = Integer.parseInt(arr[1]);
			int s = 0;
			if (arr.length == 3) {
				s = Integer.parseInt(arr[2]);
			}
			if ((h < 0) || (h > 23) || (m < 0) || (m > 59) || (s < 0) || (s > 59)) {
				return false;
			}
		} catch (Exception e) {
			return false;
		}
		return true;
	}

	public static boolean isDate(String date) {
		String[] arr = date.split("-");
		if (arr.length < 3) {
			return false;
		}
		try {
			int y = Integer.parseInt(arr[0]);
			int m = Integer.parseInt(arr[1]);
			int d = Integer.parseInt(arr[2]);
			if ((y < 0) || (m > 12) || (m < 0) || (d < 0) || (d > 31)) {
				return false;
			}
		} catch (Exception e) {
			return false;
		}
		return true;
	}

	public static boolean isDateTime(String str) {
		if (StringUtil.isEmpty(str)) {
			return false;
		}
		if (str.indexOf(" ") > 0) {
			String[] arr = str.split(" ");
			if (arr.length == 2) {
				return (isDate(arr[0])) && (isTime(arr[1]));
			}
			return false;
		}
		return isDate(str);
	}

	public static boolean isWeekend(Date date) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		int t = cal.get(7);

		return (t == 7) || (t == 1);
	}

	public static boolean isWeekend(String str) {
		return isWeekend(parse(str));
	}

	public static Date parse(String str) {
		if (StringUtil.isEmpty(str)) {
			return null;
		}
		try {
			return new SimpleDateFormat("yyyy-MM-dd").parse(str.trim());
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return null;
	}

	@Deprecated
	public static Date getDate(String str, String pattern, Locale locale) throws ParseException {
		return parse(str, pattern, locale);
	}

	@Deprecated
	public static Date getDate(String dateStr, String pattern) throws ParseException {
		return parse(dateStr, pattern);
	}

	public static Date parse(String str, String format) throws ParseException {
		return parse(str, format, Locale.getDefault());
	}

	public static Date parse(String str, String format, Locale locale) throws ParseException {
		if (StringUtil.isEmpty(str)) {
			return null;
		}
		if (str.trim().length() >= format.length()) {
			str = str.substring(0, format.length());
		}
		SimpleDateFormat t = new SimpleDateFormat(format, locale);
		return t.parse(str);
	}

	@Deprecated
	public static Date getDate(String dateStr) {
		return parse(dateStr);
	}

	@Deprecated
	public static Date getDateTime(String dateStr) {
		return parseDateTime(dateStr);
	}

	public static Date parseDateTime(String str) {
		return parseDateTime(str, Locale.getDefault());
	}

	public static Date parseDateTime(String str, Locale locale) {
		if (StringUtil.isEmpty(str)) {
			return null;
		}
		if (str.length() <= 10) {
			return parse(str);
		}
		try {
			return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", locale).parse(str);
		} catch (ParseException e) {
			try {
				return new SimpleDateFormat("yyyy-MM-dd HH:mm", locale).parse(str);
			} catch (ParseException e1) {
				try {
					return new SimpleDateFormat("yyyy-MM-dd", locale).parse(str);
				} catch (ParseException e2) {
					e2.printStackTrace();
				}
			}
		}
		return null;
	}

	public static Date parseDateTime(String str, String format) {
		if (StringUtil.isEmpty(str)) {
			return null;
		}
		try {
			SimpleDateFormat t = new SimpleDateFormat(format);
			return t.parse(str);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return null;
	}

	public static Date addSecond(Date date, long count) {
		return new Date(date.getTime() + 1000L * count);
	}

	public static Date addMinute(Date date, int count) {
		return new Date(date.getTime() + 60000L * count);
	}

	public static Date addHour(Date date, int count) {
		return new Date(date.getTime() + 3600000L * count);
	}

	public static Date addDay(Date date, int count) {
		return new Date(date.getTime() + 86400000L * count);
	}

	public static Date addWeek(Date date, int count) {
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		c.add(3, count);
		return c.getTime();
	}

	public static Date addMonth(Date date, int count) {
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		c.add(2, count);
		return c.getTime();
	}

	public static Date addYear(Date date, int count) {
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		c.add(1, count);
		return c.getTime();
	}

	public static String toDisplayDateTime(String date) {
		if (StringUtil.isEmpty(date)) {
			return null;
		}
		try {
			if (isDate(date)) {
				return toDisplayDateTime(parse(date));
			}
			SimpleDateFormat t = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			Date d = t.parse(date);
			return toDisplayDateTime(d);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return "不是标准格式时间!";
	}


	public static String toDisplayDateTime(Date date) {
		long minite = (System.currentTimeMillis() - date.getTime()) / 60000L;
		if (minite < 60L) {
			return toString(date, "MM-dd") + " " + minite + "分钟前";
		}
		if (minite < 1440L) {
			return toString(date, "MM-dd") + " " + minite / 60L + "小时前";
		}
		return toString(date, "MM-dd") + " " + minite / 1440L + "天前";
	}

	public static int getCurrentYear() {
		return getYearOfDate(new Date());
	}

	public static int getYearOfDate(Date date) {
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		return c.get(1);
	}

	public static long diffYear(Date date1, Date date2) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date1);
		int time = calendar.get(1);
		calendar.setTime(date2);
		return time - calendar.get(1);
	}

	public static long diffQuarter(Date date1, Date date2) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date1);
		int time = calendar.get(1) * 4;
		calendar.setTime(date2);
		time -= calendar.get(1) * 4;
		calendar.setTime(date1);
		time += calendar.get(2) / 4;
		calendar.setTime(date2);
		return time - calendar.get(2) / 4;
	}

	public static long diffMonth(Date date1, Date date2) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date1);
		int time = calendar.get(1) * 12;
		calendar.setTime(date2);
		time -= calendar.get(1) * 12;
		calendar.setTime(date1);
		time += calendar.get(2);
		calendar.setTime(date2);
		return time - calendar.get(2);
	}

	public static String getWeekDayByDate(String strdate) {
		String[] dayNames = { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" };
		SimpleDateFormat sdfInput = new SimpleDateFormat("yyyy-MM-dd");
		Calendar calendar = Calendar.getInstance();
		Date date = new Date();
		try {
			date = sdfInput.parse(strdate);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		calendar.setTime(date);
		int dayOfWeek = calendar.get(7) - 1;
		if (dayOfWeek < 0) {
			dayOfWeek = 0;
		}
		return dayNames[dayOfWeek];
	}

	public static long diffWeek(Date date1, Date date2) {
		Calendar calendar = Calendar.getInstance();

		calendar.setFirstDayOfWeek(2);

		calendar.setTime(date1);
		int time = calendar.get(1) * 52;
		calendar.setTime(date2);
		time -= calendar.get(1) * 52;
		calendar.setTime(date1);
		time += calendar.get(3);
		calendar.setTime(date2);
		return time - calendar.get(3);
	}

	public static Date clearHourMinuteSecond(Date date) {
		Calendar c1 = Calendar.getInstance();
		c1.setTime(date);

		c1.set(11, 0);

		c1.set(12, 0);

		c1.set(13, 0);

		c1.set(14, 0);
		return c1.getTime();
	}

	public static long diffDay(Date date1, Date date2) {
		long time = clearHourMinuteSecond(date1).getTime() / 1000L / 60L / 60L / 24L;
		return time - clearHourMinuteSecond(date2).getTime() / 1000L / 60L / 60L / 24L;
	}

	public static long diffHour(Date date1, Date date2) {
		long time = date1.getTime() / 1000L / 60L / 60L;
		return time - date2.getTime() / 1000L / 60L / 60L;
	}

	public static long diffMinute(Date date1, Date date2) {
		long time = date1.getTime() / 1000L / 60L;
		return time - date2.getTime() / 1000L / 60L;
	}

	public static long diffSecond(Date date1, Date date2) {
		long time = date1.getTime() / 1000L;
		return time - date2.getTime() / 1000L;
	}

	public static Date getDateAfter(Date date, int n, int type) {
		if (date == null) {
			return null;
		}
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		switch (type) {
		case 1:
			int year = cal.get(1);
			year += n;
			cal.set(1, year);
			break;
		case 2:
			int month = cal.get(2);
			month += n;
			cal.set(2, month);
			break;
		case 5:
			int nDate = cal.get(5);
			nDate += n;
			cal.set(5, nDate);
			break;
		case 12:
			int minute = cal.get(12);
			minute += n;
			cal.set(12, minute);
			break;
		case 11:
			int hour = cal.get(11);
			hour += n;
			cal.set(11, hour);
			break;
		case 13:
			int second = cal.get(13);
			second += n;
			cal.set(13, second);
			break;
		case 14:
			int mSecond = cal.get(14);
			mSecond += n;
			cal.set(14, mSecond);
			break;
		case 3:
		case 4:
		case 6:
		case 7:
		case 8:
		case 9:
		case 10:
		default:
			return null;
		}
		date = cal.getTime();

		return date;
	}
}
